package com.arrogames.rgblabyrinth.game {
	import com.arrogames.lib.ash.rendering.Display;
	import com.arrogames.lib.ash.rendering.Position;
	import com.arrogames.rgblabyrinth.game.graphics.Bonus;
	import com.arrogames.rgblabyrinth.game.graphics.Exit;
	import com.arrogames.rgblabyrinth.game.graphics.Player;
	import com.arrogames.rgblabyrinth.game.graphics.Start;
	import com.arrogames.rgblabyrinth.game.graphics.TiledLayer;
	import com.arrogames.rgblabyrinth.game.lighting.Vision;
	import com.arrogames.rgblabyrinth.game.targeting.PlayerMarker;
	import com.arrogames.rgblabyrinth.game.trigger.Trigger;
	import com.arrogames.rgblabyrinth.game.trigger.effect.BlinkEffect;
	import com.arrogames.rgblabyrinth.game.trigger.effect.Effect;
	import com.arrogames.tmx.StarlingMap;
	
	import flash.geom.Point;
	
	import ash.core.Engine;
	import ash.core.Entity;
	
	import starling.extensions.lighting.lights.DirectionalLight;
	import starling.extensions.lighting.lights.PointLight;
	import starling.utils.Color;

	public class EntityCreator {
		private var engine:Engine;
		
		public function EntityCreator(engine:Engine) {
			this.engine = engine;
		}
		
		public function destroyEntity(entity:Entity):void {
			engine.removeEntity(entity);
		}
		
		public function createGameState(map:StarlingMap):Entity {
			var entity:Entity = new Entity();
			entity.name = GameState.GAMESTATE;
			entity.add(new GameState(map));
			engine.addEntity(entity);
			return entity;
		}
		
		public function createPlayer(x:Number, y:Number, vision:Number):Entity {
			var entity:Entity = new Entity();
			var graphics:Player = new Player();
			entity.add(new Position(x, y, 2));
			entity.add(new Display(graphics));
			entity.add(new Vision(vision));
			entity.add(new PlayerMarker());
			entity.add(new PointLight(x, y, vision, Color.WHITE, 1));
			
			engine.addEntity(entity);
			return entity;
		}
		
		public function createStart(x:Number, y:Number, tileWidth:int, tileHeight:int):Entity {
			var entity:Entity = new Entity();
			entity.add(new Position(x,y,0));
			entity.add(new Display(new Start()));
			
			engine.addEntity(entity);
			return entity;
		}
		
		public function createExit(x:Number, y:Number, tileWidth:int, tileHeight:int):Entity {
			var entity:Entity = new Entity();
			entity.add(new Position(x,y,0));
			entity.add(new Display(new Exit()));
			
			engine.addEntity(entity);
			return entity;
		}
		
		public function createTiledWorld(starlingMap:StarlingMap): void {
			
			for each (var tiledLayer:TiledLayer in starlingMap.tiledLayers) {
				var entity:Entity = new Entity();
				entity.add(new Position(0,0,0));
				entity.add(new Display(tiledLayer));
				engine.addEntity(entity);
			}
		}
		
		public function createBonus(x:Number, y:Number, tileWidth:int, tileHeight:int):Entity {
			var entity:Entity = new Entity();
			entity.add(new Position(x,y,0));
			entity.add(new Display(new Bonus()));
			
			engine.addEntity(entity);
			return entity;
		}
		
		public function addTrigger(begin:Point, end:Point):Entity {
			
			var entity2:Entity = new Entity();
			entity2.add(new Position(0, 0, 0));
			var vision:Vision = new Vision(0,.8);
			entity2.add(vision);
			entity2.add(new DirectionalLight(0, Color.WHITE, .8));
			engine.addEntity(entity2);
			
			
			var entity:Entity = new Entity();
			entity.add(new Trigger(begin, end));
			entity.add(new Effect(new BlinkEffect("GoDark", entity2, vision), 1));
			
			engine.addEntity(entity);
			return entity;
			
		}
	}
}